<script>
const urlParams = new URLSearchParams(window.location.search);
const oldFromEmail = urlParams.get('old'); // param set by `forget password` email.
const nameFromEmail = urlParams.get('name'); // param set by `forget password` email.
let name = getCookie('name');
if(oldFromEmail != null) {
    document.getElementById("inputOld").value = oldFromEmail;
    document.getElementById("inputOld").type = "text";
    name = nameFromEmail;
}
if(name == "" || name == null) {
    window.location.href = "/login.html";
}
document.getElementById("idHead1").innerText = "Change or Reset password for " + name;
function changePassword() {
    let old = document.getElementById("inputOld").value;
    let new_ = document.getElementById("inputNew").value;
    let confirm = document.getElementById("inputConfirm").value;
    if(old == "" || new_ == "") {
        alert("Please fill the form.");return;
    }
    if(new_ != confirm) {
        alert("Wrong password confirm");return;
    }
    new_ = sha256("Jingwww" + new_ + "Jingwww")
    if(oldFromEmail != old) { // password from email is already hashed.
        old = sha256("Jingwww" + old + "Jingwww")
    }
    let res = httpGetSync("/api/ChangePassword?old={0}&new={1}&name={2}".format(old, new_, name));
    if(res == "status=ok") {
        alert("Done.");
    } else {
        alert("Failed: " + res);
    }
}
</script>

<section class="section">
    <div class="container">
        <h1 class="title" id="idHead1">Change or Reset password</h1>
        <div class="field">
            <label class="label noselect">Old Password</label><br />
            <input class="input" type="password" id="inputOld">
        </div>
        <div class="field">
            <label class="label noselect">New Password</label><br />
            <input class="input" type="password" id="inputNew">
        </div>
        <div class="field">
            <label class="label noselect">New Password Confirm</label><br />
            <input class="input" type="password" id="inputConfirm">
        </div>

        <br />
        <button type="submit" class="button is-primary" onclick="changePassword();">ChangePassword</button>
    </div>
</section>

